Android发送接收短信
package com.example.demo;import android.Manifest;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageManager;import android.graphics.Color;import android.os.Build;import android.os.Bundle;import android.telephony.SmsManager;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import androidx.core.app.ActivityCompat;import androidx.core.content.ContextCompat;import java.util.List;/** * 发送短信Demo * @description: * @author ldm * @date 2016-4-22 上午9:07:53 */public class SmsActivity extends Activity implements View.OnClickListener { public static final String SMS_RECIPIENT_EXTRA = "com.example.demo.SMS_RECIPIENT"; public static final String ACTION_SMS_SENT = "com.example.demo.SMS_SENT_ACTION"; private Button sms_send_message; private EditText sms_recipient; private EditText sms_content; private CheckBox sms_enable_receiver; private TextView sms_status; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS} , 1); setContentView(R.layout.send_sms_layout); initViews(); initListeners(); registerSmsReceiver(); } /** * 注冊短信监听广播 * * @description: * @author ldm * @date 2016-4-22 上午9:23:23 */ private void registerSmsReceiver() { registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String message = "null"; boolean error = true; switch (-1) { case Activity.RESULT_OK: message = "发送成功!"; error = false; break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: message = "发送失败"; break; case SmsManager.RESULT_ERROR_NO_SERVICE: message = "发送失败"; break; case SmsManager.RESULT_ERROR_NULL_PDU: message = "发送失败"; break; case SmsManager.RESULT_ERROR_RADIO_OFF: message = "发送失败"; break; } sms_recipient.setEnabled(true); sms_content.setEnabled(true); sms_content.setText(""); sms_status.setText(message); sms_status.setTextColor(error ? Color.RED : Color.GREEN); } }, new IntentFilter(ACTION_SMS_SENT)); } /** * 监听事件 * * @description: * @author ldm * @date 2016-4-22 上午9:24:20 */ private void initListeners() { sms_send_message.setOnClickListener(this); // 应用Package管理器 final PackageManager pm = this.getPackageManager(); // 获取到应用中指定的组件 final ComponentName componentName = new ComponentName("com.example.demo","com.example.demo.MyMsmReceiver"); // 依据组件是否可用的状态来推断sms_enable_receiver的选中状态 sms_enable_receiver .setChecked(pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED); // 监听sms_enable_receiver设置广播组件是否可用 sms_enable_receiver .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 组件状态设置 pm.setComponentEnabledSetting( componentName, isChecked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } }); } private void initViews() { sms_recipient = (EditText) (findViewById(R.id.sms_recipient)); sms_content = ((EditText) findViewById(R.id.sms_content)); // 是否打开短信广播接收开关 sms_enable_receiver = (CheckBox) findViewById(R.id.sms_enable_receiver); sms_send_message = (Button) findViewById(R.id.sms_send_message); sms_status = (TextView) findViewById(R.id.sms_status); if (getIntent().hasExtra(SMS_RECIPIENT_EXTRA)) { sms_recipient.setText(getIntent().getExtras().getString( SMS_RECIPIENT_EXTRA)); sms_content.requestFocus(); } } @Override public void onClick(View v) { if (v.getId() == R.id.sms_send_message) { sendSms(); } } /** * 发送短信 * * @description: * @author ldm * @date 2016-4-22 上午9:30:38 */ private void sendSms() { if (TextUtils.isEmpty(sms_recipient.getText())) { Toast.makeText(SmsActivity.this, "请输入短信接收者", Toast.LENGTH_SHORT) .show(); return; } if (TextUtils.isEmpty(sms_content.getText())) { Toast.makeText(SmsActivity.this, "请输入短信内容", Toast.LENGTH_SHORT) .show(); return; } sms_recipient.setEnabled(false); sms_content.setEnabled(false); SmsManager sms = SmsManager.getDefault(); List<String> messages = sms.divideMessage(sms_recipient.getText() .toString()); String recipient = sms_content.getText().toString(); for (String message : messages) { // 发送短信 sms.sendTextMessage(recipient, null, message, PendingIntent .getBroadcast(SmsActivity.this, 0, new Intent( ACTION_SMS_SENT), 0), null); } }}
评论